package com.paireach.bw.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.paireach.bw.R;
import com.paireach.bw.common.Constant;
import com.paireach.bw.utils.LogUtil;

import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelector;

/**
 * Created by ${cqc} on 2017/8/28.
 */

public class KaRejectGridViewAdapter extends BaseAdapter {

    private List<String> pathList;
    private Context context;
    int mItemWidth;

    public KaRejectGridViewAdapter(Context context, List<String> pathList) {
        this.context = context;
        this.pathList = pathList;

        WindowManager wm = (WindowManager) (context).getSystemService(Context.WINDOW_SERVICE);
        int width = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Point size = new Point();
            wm.getDefaultDisplay().getSize(size);
            width = size.x;
        } else {//getWidth()在API>=13后 过时了，
            width = wm.getDefaultDisplay().getWidth();
        }
        mItemWidth = (width - 20) / 3;
    }

    @Override
    public int getCount() {//最多选择3张图片
        if (pathList.size() == Constant.Integer_3) {
            return Constant.Integer_3;
        }
        return pathList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gridview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ViewGroup.LayoutParams params = convertView.getLayoutParams();
        params.width = mItemWidth;
        params.height = mItemWidth;
        convertView.setLayoutParams(params);

        if (position == pathList.size()) {
            Glide.with(parent.getContext()).load(R.mipmap.add).into(holder.iv_photo);
            holder.iv_delete.setVisibility(View.GONE);
            holder.iv_photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MultiImageSelector.create().count(Constant.Integer_3).start((Activity) context, Constant.Integer_120);
                }
            });
        } else {
            Glide.with(parent.getContext()).load("file://" + pathList.get(position)).into(holder.iv_photo);
            holder.iv_photo.setClickable(false);
            holder.iv_delete.setVisibility(View.VISIBLE);
            holder.iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pathList.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
        return convertView;
    }

    public class ViewHolder {

        ImageView iv_photo;
        ImageView iv_delete;

        public ViewHolder(View itemView) {
            super();
            iv_photo = (ImageView) itemView.findViewById(R.id.image_photo);
            iv_delete = (ImageView) itemView.findViewById(R.id.iv_delete);
        }
    }
}
